package com.borrow.jiemihua.mainUI.activity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.borrow.jiemihua.R;
import com.borrow.jiemihua.base.BaseActivity;
import com.borrow.jiemihua.base.BaseConstant;
import com.borrow.jiemihua.base.BaseRVAdapter;
import com.borrow.jiemihua.base.BaseViewHolder;
import com.borrow.jiemihua.base.URLConstant;
import com.borrow.jiemihua.bean.MessageMyBean;
import com.borrow.jiemihua.utils.SharedPreferencesUtils;
import com.borrow.jiemihua.utils.xutils3.MyCallBack;
import com.borrow.jiemihua.utils.xutils3.XUtil;
import com.borrow.jiemihua.view.pull.PullLoadMoreRecyclerView;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@ContentView(R.layout.activity_my_message)
public class MyMessageActivity extends BaseActivity {
    @ViewInject(R.id.tv_top_title)
    private TextView tv_top_title;
    private String typeId;
    @ViewInject(R.id.recyclerView)
    private PullLoadMoreRecyclerView recyclerView;
    private List<MessageMyBean.DataBean> dataBeanList=new ArrayList<>();
    private BaseRVAdapter rvAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        x.view().inject(this);
        initData();
        initView();
        setRecyclerView();



    }

    @Override
    protected void initData() {
        tv_top_title.setText("我的消息");
        typeId=getIntent().getStringExtra("typeId");
    }

    @Override
    protected void initView() {
        recyclerView.setLinearLayout();

    }
    @Event(value = {R.id.imgbtn_left}, type = View.OnClickListener.class)
    private void getEvent(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.imgbtn_left:
                finish();
                break;
            default:break;

        }
    }
    public void setRecyclerView() {
        rvAdapter=new BaseRVAdapter(this,dataBeanList) {
            @Override
            public int getLayoutId(int viewType) {
                return R.layout.messsger_recyitem;
            }

            @Override
            public void onBind(BaseViewHolder holder, final int position) {
                holder.getTextView(R.id.tv_title).setText(dataBeanList.get(position).getTitle());
                if(dataBeanList.get(position).getIsRead()==0){
                    holder.getTextView(R.id.tv_tz).setVisibility(View.VISIBLE);
                }else{
                    holder.getTextView(R.id.tv_tz).setVisibility(View.INVISIBLE);
                }
                holder.getView(R.id.ll_item_all).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        startActivity(new Intent(MyMessageActivity.this,PlatformDetailsActivity.class)
                        .putExtra("terraceId",dataBeanList.get(position).getContent()));
                    }
                });

            }

        };
        recyclerView.setAdapter(rvAdapter);
        upMessageData(1);
        recyclerView.setOnPullLoadMoreListener(new PullLoadMoreRecyclerView.PullLoadMoreListener() {

            @Override
            public void onRefresh() {
                upMessageData(1);
            }

            @Override
            public void onLoadMore() {
                upMessageData(2);
            }
        });
    }
    private int page;
    private void upMessageData(final int i) {
        Map<String,String> map=new HashMap<>();
        String userid = SharedPreferencesUtils.getString(this, BaseConstant.SPConstant.USERID, "");
        map.put("userId",userid);
        if (i == 1) {
            page = 1;
        } else {
            page++;
        }
        map.put("page",page+"");
        map.put("pageSize",10+"");
//        showDialog();
        XUtil.Get(URLConstant.MY_MESSAGE,map,new MyCallBack<String>(){
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Log.i("==我的消息",result);
                try {
                    JSONObject jsonObject = new JSONObject(result);
                    String res = jsonObject.optString("isSuccess");
                    if(res.equals("1")){
                        Gson gson=new Gson();
                        MessageMyBean messageBean=gson.fromJson(result,MessageMyBean.class);
                        List<MessageMyBean.DataBean> dataBean=messageBean.getData();
                        if (i == 1) {
                            dataBeanList.clear();
                        }
                        rvAdapter.addDataLs(dataBean);
                        rvAdapter.notifyDataSetChanged();
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished() {
                super.onFinished();
                recyclerView.setPullLoadMoreCompleted();
//                closeDialog();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
                ex.printStackTrace();
//                closeDialog();
            }
        });
    }

}
